package cn.getech.data.development.utils.sap;

import cn.getech.data.development.entity.ConfConnect;
import cn.getech.data.intelligence.common.exception.RRException;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.ext.DestinationDataProvider;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;

public class SAPConnUtils {

    private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";

    /**
     * 创建SAP接口属性文件。
     * @param name  ABAP管道名称
     * @param suffix    属性文件后缀
     * @param properties    属性文件内容
     */
    private static void createDataFile(String name, String suffix, Properties properties){
        File cfg = new File(name+"."+suffix);
        if(cfg.exists()){
            cfg.deleteOnExit();
        }
        try{
            FileOutputStream fos = new FileOutputStream(cfg, false);
            properties.store(fos, "for tests only !");
            fos.close();
        }catch (Exception e){
            System.out.println("Create Data file fault, error msg: " + e.toString());
            throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
        }
    }

    /**
     * 初始化SAP连接
     */
    private static void initProperties(ConfConnect confConnect) {
        Properties connectProperties = new Properties();
        if(null == confConnect.getSapConnectFieldInfo().getIsGroup() || 0 == confConnect.getSapConnectFieldInfo().getIsGroup()){
            // SAP服务器
            connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, confConnect.getHost());
            // SAP系统编号
            connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, confConnect.getSapConnectFieldInfo().getSysnr());
        }else if(null != confConnect.getSapConnectFieldInfo().getIsGroup() && 1 == confConnect.getSapConnectFieldInfo().getIsGroup()){
            connectProperties.setProperty(DestinationDataProvider.JCO_MSHOST, confConnect.getHost());
            connectProperties.setProperty(DestinationDataProvider.JCO_GROUP, confConnect.getSapConnectFieldInfo().getGroupName());
            connectProperties.setProperty(DestinationDataProvider.JCO_R3NAME, confConnect.getSapConnectFieldInfo().getR3Name());
        }
        // SAP集团
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, confConnect.getPort());
        // SAP用户名
        connectProperties.setProperty(DestinationDataProvider.JCO_USER,   confConnect.getUsername());
        // SAP密码
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, confConnect.getPassword());
        // SAP登录语言
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG,  confConnect.getSapConnectFieldInfo().getLang());
//        // 最大连接数
//        connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, sapConn.getJCO_POOL_CAPACITY());
//        // 最大连接线程
//        connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, sapConn.getJCO_PEAK_LIMIT());
//        // SAP ROUTER
//        connectProperties.setProperty(DestinationDataProvider.JCO_SAPROUTER, sapConn.getJCO_SAPROUTER());
        createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);
    }


    /**
     * 初始化SAP连接
     */
    private static void initProperties1() {
        Properties connectProperties = new Properties();
        // SAP服务器
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, SapConn.jcoAshost);
//        connectProperties.setProperty(DestinationDataProvider.JCO_MSHOST, SapConn.jcoAshost);
//        connectProperties.setProperty(DestinationDataProvider.JCO_MSSERV, "3200");
        // SAP系统编号
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  SapConn.jcoSysnr);
        // SAP集团
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, SapConn.jcoClient);
        // SAP用户名
        connectProperties.setProperty(DestinationDataProvider.JCO_USER,  SapConn.jcoUser);
        // SAP密码
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, SapConn.jcoPassword);
        // SAP登录语言
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG,  SapConn.jcoLang);
//        // 最大连接数
        connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3");
        // 最大连接线程
        connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10");
//        // SAP ROUTER
//        connectProperties.setProperty(DestinationDataProvider.JCO_SAPROUTER, sapConn.getJCO_SAPROUTER());
        //最大连接时间
        connectProperties.setProperty(DestinationDataProvider.JCO_MAX_GET_TIME, "57600");
        createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);
    }

    /**
     * 获取SAP连接
     * @return  SAP连接对象
     */
    public static JCoDestination connect(ConfConnect confConnect){
        System.out.println("正在连接至SAP..." + confConnect.getId());
        JCoDestination destination = null;
        initProperties(confConnect);
        try {
            destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
            destination.ping();
            System.out.println("已成功建立sap的连接");
        } catch (JCoException e) {
            throw new RRException("Connect SAP fault, error msg: " + e.toString());
        }
        return destination;
    }

    /**
     * 获取SAP连接
     * @return  SAP连接对象
     */
    public static JCoDestination connect1(){
        System.out.println("正在连接至SAP...");
        JCoDestination destination = null;
        initProperties1();
        try {
            destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
            destination.ping();
            System.out.println("已成功建立sap的连接");
        } catch (JCoException e) {
            throw new RRException("Connect SAP fault, error msg: " + e.toString());
        }
        return destination;
    }
}
